package com.xnfh.home.ui.fragment

import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.xnfh.framework.base.BaseFragment
import com.xnfh.framework.base.BaseStateObserver
import com.xnfh.framework.bean.PlanScheme
import com.xnfh.home.R
import com.xnfh.home.adapter.PlantingSchemeAdapter
import com.xnfh.home.databinding.FragmentPlantingSchemeBinding
import com.xnfh.home.viewmodel.ClubDetailViewModel

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2024/1/5 13:44
 * @UpdateUser: ziheng
 * @UpdateDate: 2024/1/5 13:44
 * @UpdateRemark: 更新说明
 */
class PlantingSchemeFragment: BaseFragment<FragmentPlantingSchemeBinding>() {
    override fun getLayoutID(): Int = R.layout.fragment_planting_scheme

    private lateinit var viewModel: ClubDetailViewModel
    private lateinit var adapter: PlantingSchemeAdapter
    private var page = 1

    override fun initView(view: View, savedInstanceState: Bundle?) {
        val bundle = activity?.intent?.getBundleExtra("bundle")
        val cropId = bundle?.getString("cropId",null)
        mBind.plantingSchemeRecycle.layoutManager = LinearLayoutManager(requireContext())
        adapter = PlantingSchemeAdapter()
        mBind.plantingSchemeRecycle.adapter = adapter
        mBind.plantingSchemeSmartRefresh.autoRefresh()
        mBind.plantingSchemeSmartRefresh.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel.requestPlanScheme(cropId,mBind.plantingSchemeEdit.text.toString(),page)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
            }

        })

        mBind.plantingSchemeEdit.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                mBind.plantingSchemeSmartRefresh.autoRefresh()
            }
            val imm = requireActivity().getSystemService(AppCompatActivity.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(v.windowToken, 0) //强制隐藏键盘
            return@setOnEditorActionListener false
        }
        mBind.plantingSchemeEdit.setOnDeleteClickListener {
            mBind.plantingSchemeSmartRefresh.autoRefresh()
        }
    }

    override fun observe() {
        viewModel = ClubDetailViewModel()
        viewModel.plantSchemeList.observe(this,object : BaseStateObserver<PlanScheme>(){
            override fun getRespDataSuccess(it: PlanScheme) {
                super.getRespDataSuccess(it)
                mBind.plantingSchemeSmartRefresh.let {
                    it.finishLoadMore()
                    it.finishRefresh()
                }
                if(it.total == 0){
                    adapter.setEmptyView(com.xnfh.common.R.layout.adapter_empty_view)
                    return
                }
                if(page == 1){
                    adapter.setNewInstance(it.list.toMutableList())
                }else{
                    adapter.addData(it.list)
                }
            }
        })
    }
}